#ifndef EVENTDRIVEN_H
#define EVENTDRIVEN_H

#ifdef __cplusplus
extern "C" {
#endif 

#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <time.h>

typedef enum {
    EVENT_HDL_SUCCESS = 0,
    EVENT_HDL_ERR_PARAM,
    EVENT_HDL_ERR_MEM,
    EVENT_HDL_ERR_FULL,
    EVENT_HDL_ERR_EMPTY
} EventHdlStatus;

typedef struct EventHandler {
    int type; // 事件类型
    void (*callback)(void *); // 事件对应的回调函数
} EventHandler;

typedef struct Event {
    time_t eventSTime;
    int type; // 事件类型
    void *param; // 事件参数
    int size; // 事件参数大小
} Event;

typedef struct EventQueue {
    Event* events; // 事件队列
    int head;
    int tail;
    int size;
    int count; // 当前缓冲区中的元素数量
    sem_t sem; // 队列信号量
    pthread_mutex_t mutex; // 操作互斥锁
} EventQueue;

typedef struct EventEmitter {
    EventQueue* queue; // 事件队列
    EventHandler* handlers; // 事件处理器
    int handleSize;
} EventEmitter;

int initEventEmitter(EventEmitter *events, EventHandler* handlers, int eventSize, int handleSize);
void distoryEventEmitter(EventEmitter *events);
int pushEvent(EventEmitter *events, const Event event);
int popEvent(EventEmitter *events, Event* event);
int handleEventOnce(EventEmitter *events);
void *handleEvent(void *events);

#ifdef __cplusplus
}
#endif 

#endif
